﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.TeamFoundation.Build.Workflow.Activities;

namespace HybridLab.Workflow.Activities.UI
{
	internal partial class BuildDetailsPage : UserControl
	{
		private ErrorProvider buildDefinitionErrorProvider;
		private ErrorProvider configurationErrorProvider;
		private ErrorProvider customBuildPathErrorProvider;

		private HybridLabWizardData wizardData;
		private bool loadingBuilds;
		private bool loadingConfigurations;

		private BuildDetailsPage()
		{
			InitializeComponent();
		}

		internal BuildDetailsPage(HybridLabWizardData wizardData)
			: this()
		{
			if (wizardData == null)
			{
				throw new ArgumentNullException("wizardData");
			}
			Enabled = false;
			this.wizardData = wizardData;
			wizardData.DataLoadCompleted += new EventHandler(wizardData_DataLoadCompleted);
		}

		private void AttachHandlers()
		{
			browseButton.Click += new EventHandler(browseButton_Click);
			buildDefinitionComboBox.SelectedIndexChanged += new EventHandler(buildDefinitionComboBox_SelectedIndexChanged);
			buildComboBox.SelectedIndexChanged += new EventHandler(buildComboBox_SelectedIndexChanged);
			configurationComboBox.SelectedIndexChanged += new EventHandler(configurationComboBox_SelectedIndexChanged);
			queueNewBuildRadioButton.CheckedChanged += new EventHandler(queueNewBuildRadioButton_CheckedChanged);
			chooseExistingBuildRadioButton.CheckedChanged += new EventHandler(chooseExistingBuildRadioButton_CheckedChanged);
			teamBuildRadioButton.CheckedChanged += new EventHandler(teamBuildRadioButton_CheckedChanged);
			customBuildRadioButton.CheckedChanged += new EventHandler(customBuildRadioButton_CheckedChanged);
			customPathTextBox.TextChanged += new EventHandler(customPathTextBox_TextChanged);
			wizardData.BuildDetails.ErrorChanged += new EventHandler(BuildDetails_ErrorChanged);
		}

		private void browseButton_Click(object sender, EventArgs e)
		{
			using (var diag = new FolderBrowserDialog())
			{
				diag.SelectedPath = customPathTextBox.Text;
				if (DialogResult.OK == diag.ShowDialog(this))
				{
					customPathTextBox.Text = diag.SelectedPath;
				}
			}
		}

		private void buildComboBox_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (!loadingBuilds)
			{
				wizardData.BuildDetails.BuildDetail = buildComboBox.SelectedItem as BuildDetailEx;
				UpdateConfigurations();
			}
		}

		private void buildDefinitionComboBox_SelectedIndexChanged(object sender, EventArgs e)
		{
			try
			{
				wizardData.BuildDetails.BuildDefinition = buildDefinitionComboBox.SelectedItem as BuildDefinitionEx;
				UpdateBuilds();
				UpdateConfigurations();
			}
			catch (Exception exception)
			{
				OnError(exception);
			}
		}

		private void BuildDetails_ErrorChanged(object sender, EventArgs e)
		{
			RefreshErrors();
		}

		private void chooseExistingBuildRadioButton_CheckedChanged(object sender, EventArgs e)
		{
			SetTeamSystemBuildSelectionMode(!chooseExistingBuildRadioButton.Checked);
			UpdateConfigurations();
		}

		private void configurationComboBox_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (!loadingConfigurations)
			{
				wizardData.BuildDetails.Configuration = configurationComboBox.SelectedItem as PlatformConfiguration;
			}
		}

		private void customBuildRadioButton_CheckedChanged(object sender, EventArgs e)
		{
			SetBuildSelectionMode(!customBuildRadioButton.Checked);
		}

		private void customPathTextBox_TextChanged(object sender, EventArgs e)
		{
			wizardData.BuildDetails.CustomBuildPath = customPathTextBox.Text;
		}

		private void Initialize()
		{
			try
			{
				buildDefinitionComboBox.DisplayMember = "Name";
				buildDefinitionComboBox.DataSource = wizardData.BuildDefinitions;
				buildDefinitionComboBox.SelectedItem = null;
				UpdateUI();
				InitializeErrorProviders();
				RefreshErrors();
				AttachHandlers();
			}
			catch (Exception exception)
			{
				OnError(exception);
			}
		}

		private void InitializeErrorProviders()
		{
			buildDefinitionErrorProvider = WizardUtils.CreateErrorProvider(buildDefinitionComboBox);
			customBuildPathErrorProvider = WizardUtils.CreateErrorProvider(customPathTextBox);
			configurationErrorProvider = WizardUtils.CreateErrorProvider(configurationComboBox);
		}

		private void wizardData_DataLoadCompleted(object sender, EventArgs e)
		{
			Enabled = true;
			Initialize();
		}

		private void OnError(Exception ex)
		{
		}

		private void queueNewBuildRadioButton_CheckedChanged(object sender, EventArgs e)
		{
			SetTeamSystemBuildSelectionMode(queueNewBuildRadioButton.Checked);
			UpdateConfigurations();
		}

		private void RefreshErrors()
		{
			string err = wizardData.BuildDetails["BuildDefinition"];
			buildDefinitionErrorProvider.SetError(buildDefinitionComboBox, err);
			err = wizardData.BuildDetails["CustomBuildPath"];
			customBuildPathErrorProvider.SetError(customPathTextBox, err);
			err = wizardData.BuildDetails["Configuration"];
			configurationErrorProvider.SetError(configurationComboBox, err);
		}

		private void SetBuildSelectionMode(bool isTeamSystemBuild)
		{
			teamBuildLayoutPanel.Enabled = isTeamSystemBuild;
			teamBuildRadioButton.Checked = isTeamSystemBuild;
			customBuildLayoutPanel.Enabled = !isTeamSystemBuild;
			customBuildRadioButton.Checked = !isTeamSystemBuild;
			wizardData.BuildDetails.IsTeamSystemBuild = isTeamSystemBuild;
		}

		private void SetTeamSystemBuildSelectionMode(bool isQueueNewBuild)
		{
			queueNewBuildRadioButton.Checked = isQueueNewBuild;
			chooseExistingBuildRadioButton.Checked = !isQueueNewBuild;
			buildComboBox.Enabled = !isQueueNewBuild;
			wizardData.BuildDetails.QueueNewBuild = isQueueNewBuild;
		}

		private void teamBuildRadioButton_CheckedChanged(object sender, EventArgs e)
		{
			SetBuildSelectionMode(teamBuildRadioButton.Checked);
		}

		private void UpdateBuilds()
		{
			loadingBuilds = true;
			buildComboBox.DisplayMember = "Name";
			buildComboBox.DataSource = wizardData.BuildDetails.BuildList;
			buildComboBox.SelectedItem = wizardData.BuildDetails.BuildDetail;
			loadingBuilds = false;
		}

		private void UpdateConfigurations()
		{
			try
			{
				loadingConfigurations = true;
				configurationComboBox.DataSource = wizardData.BuildDetails.ConfigurationList;
				configurationComboBox.SelectedItem = wizardData.BuildDetails.Configuration;
				bool flag = (wizardData.BuildDetails.ConfigurationList != null) && (wizardData.BuildDetails.ConfigurationList.Count > 0);
				configurationComboBox.Enabled = flag;
				buildConfigDetailLabel.Enabled = flag;
			}
			catch (Exception exception)
			{
				OnError(exception);
			}
			finally
			{
				loadingConfigurations = false;
			}
		}

		private void UpdateUI()
		{
			BuildDetailsEx buildDetails = wizardData.BuildDetails;
			SetBuildSelectionMode(buildDetails.IsTeamSystemBuild);
			SetTeamSystemBuildSelectionMode(buildDetails.QueueNewBuild);
			if (buildDetails.BuildDefinition != null)
			{
				foreach (BuildDefinitionEx ex in buildDefinitionComboBox.Items)
				{
					if (ex.Definition.Uri == buildDetails.BuildDefinition.Definition.Uri)
					{
						buildDefinitionComboBox.SelectedItem = ex;
						break;
					}
				}
			}
			UpdateBuilds();
			UpdateConfigurations();
			customPathTextBox.Text = buildDetails.CustomBuildPath;
		}
	}
}
